/*
 * Created by JFormDesigner on Sun Jun 30 10:34:52 HKT 2024
 */

package com.hongyan.view;

import com.hongyan.base.BaseContentJpanel;
import com.hongyan.base.BaseJpanel;
import com.hongyan.bean.UserFunc;
import com.hongyan.config.MainFrameConfig;
import com.hongyan.main.SwingMainFrame;
import com.hongyan.util.TypeUtils;

import java.lang.Class;
import java.awt.*;
import java.util.List;
import java.util.stream.Collectors;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * @author hongyan
 */
public class SwingContentPanel extends BaseJpanel {

    private int type;

    private String id;
    private List<UserFunc> funcList;
    private int lastSelectedIndex = 0;

    private SwingContentPanel(SwingMainFrame swingMainFrame) {
        super(swingMainFrame);
    }

    public SwingContentPanel(SwingMainFrame swingMainFrame, int type1, String id) {
        this(swingMainFrame);

        this.id = id;
        MainFrameConfig.setId(id);
        this.type = type1;


        initData();

    }

    public void replacePanel(BaseContentJpanel panel) {
        contentPanel.removeAll();

        contentPanel.add(panel);
        // 更新界面
        contentPanel.revalidate();
        contentPanel.repaint();

    }

    @Override
    public void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        scrollPane1 = new JScrollPane();
        list1 = new JList();
        contentPanel = new JPanel();

        //======== this ========
        setLayout(null);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(list1);
        }
        add(scrollPane1);
        scrollPane1.setBounds(0, 0, 100, 600);

        //======== contentPanel ========
        {
            contentPanel.setLayout(null);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < contentPanel.getComponentCount(); i++) {
                    Rectangle bounds = contentPanel.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = contentPanel.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                contentPanel.setMinimumSize(preferredSize);
                contentPanel.setPreferredSize(preferredSize);
            }
        }
        add(contentPanel);
        contentPanel.setBounds(100, 0, 700, 600);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < getComponentCount(); i++) {
                Rectangle bounds = getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            setMinimumSize(preferredSize);
            setPreferredSize(preferredSize);
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    @Override
    public void initData() {
        funcList = TypeUtils.getFuncListByType(type);
        Object[] array = funcList.stream().map(UserFunc::getFuncname).toArray();
        list1.setListData(array);
        try {
            replacePanel((BaseContentJpanel) funcList.get(0).getContentJpanel().newInstance());
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void initListen() {
        list1.setCellRenderer(new DefaultListCellRenderer() {
            private static final long serialVersionUID = 1L;

            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (c instanceof JLabel) {
                    JLabel label = (JLabel) c;
                    label.setBorder(new EmptyBorder(5, 5, 5, 5));
                    label.setBackground(isSelected ? Color.GRAY : list.getBackground());
                    label.setForeground(isSelected ? list.getBackground() : Color.BLUE);
                    label.setText("<html><b>" + label.getText() + "</b></html>");
                }
                return c;
            }
        });
        list1.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                int selectedIndex = list1.getSelectedIndex();
                if (selectedIndex != lastSelectedIndex) {
                    try {
                        replacePanel((BaseContentJpanel) funcList.get(selectedIndex).getContentJpanel().newInstance());
                    } catch (InstantiationException | IllegalAccessException ex) {
                        throw new RuntimeException(ex);
                    }
                    lastSelectedIndex = selectedIndex;
                }
            }


        });
    }

    @Override
    public int getJpanelHeight() {
        return 620;
    }

    @Override
    public int getJpanelWidth() {
        return 820;
    }

    @Override
    public String title() {
        return "欢迎使用软件测试管理系统，" + (type == 0 ? "学生：" + id : type == 1 ? "老师：" + id : "管理员：" + id);
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JScrollPane scrollPane1;
    private JList list1;
    private JPanel contentPanel;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
